package com.example.interfacecall.handle;

import com.example.interfacecall.domain.Result;
import com.example.interfacecall.exception.MyException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@RestControllerAdvice
public class CustomExtHandle {
    // 捕获全局异常
    @ExceptionHandler(value = Exception.class)
    Object handleException(Exception e, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", e.getMessage());
        map.put("url", request.getRequestURL());
        return map;
    }

    // 如果是Myexception类
    @ExceptionHandler(value = MyException.class)
    Object handleMyException(MyException e, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error.html"); // 指定错误跳转页面 需要在templates里面新建 一个error.html
        modelAndView.addObject("msg", e.getMsg());
        modelAndView.addObject("code", e.getCode());
        modelAndView.addObject("url", request.getRequestURL());
        return modelAndView;

        // 当然这里也可以返回json数据 前后台分离的话直接返回一个json即可
    }

    //处理Get请求中 使用@Valid 验证路径中请求实体校验失败后抛出的异常
//    @ExceptionHandler(BindException.class)
//    @ResponseBody
//    public Result BindExceptionHandler(BindException e) {
//        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
//        return Result.error(message);
//    }
//
//    //处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是javax.validation.ConstraintViolationException
//    @ExceptionHandler(ConstraintViolationException.class)
//    @ResponseBody
//    public Result ConstraintViolationExceptionHandler(ConstraintViolationException e) {
//        String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining());
//        return Result.error(message);
//    }
//
//    //处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常。
//    @ExceptionHandler(MethodArgumentNotValidException.class)
//    @ResponseBody
//    public Result MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
//        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
//        return Result.error(message);
//    }

}
